package main.test.sparklingGraphAPI

import ml.sparkling.graph.operators.OperatorsDSL._
import ml.sparkling.graph.api.operators.algorithms.coarsening.CoarseningAlgorithm.Component
import org.apache.spark.SparkContext
import org.apache.spark.graphx.{Graph, VertexId}
import ml.sparkling.graph.loaders.csv.GraphFromCsv.CSV
import main.test.sparklingGraphAPI.LoadingGraph.ctx
import ml.sparkling.graph.api.loaders.GraphLoading.LoadGraph
import ml.sparkling.graph.operators.algorithms.coarsening.labelpropagation.VertexWrapper

object GraphCoarsening {

  def main(args: Array[String]): Unit = {
    //Graph coarsening

    /**
     * In order to limit computation,
     * you can decrease graph size
     * using coarsening operator.
     * New graph will be smaller
     * because neighborhood vertices
     * will be coarsed into single vertices.
     *
     * Edges are created using edges from input graph,
     * filtering self loops.
     *
     */

    //Label propagation based graph coarsening
    /**
     * One of implementation is based on label propagation.
     * Implementation propagates vertex identifier to neighbours.
     * Neighbours groups them and sorts by number of occurences.
     * if number of occurrences is same, minimal one is selected (in order to gurante deterministic execution).
     * Otherwise, vertex identifier with biggest number of occurencies(or minmal one in case of same ocurrencies number)
     * is selected.
     *
     */

    //load your graph (for example using Graph loading API)
//    val filePath="src/main/test/Douban-dataset/data/edges.csv"
    val filePath="data/your_graph_path.csv"

    val graph: Graph[String, String] =LoadGraph.from(CSV(filePath)).load()
    var maxIters = 4
    var coarsedGraph: Graph[Component, _]   = graph.LPCoarse()
    // 循环计算它的简化图
    for (i <- 1 to maxIters)
//    while (maxIters>0)
      {
        coarsedGraph  = coarsedGraph.LPCoarse()
        maxIters=maxIters-1
        println(coarsedGraph.edges.collect().length)
        coarsedGraph.edges.foreach(println(_))
      }



    println(coarsedGraph.edges.collect().length)

    //  You can also coarse graph treated as undirected one

    val coarsedGraph2 = graph.LPCoarse(treatAsUndirected = true)
    coarsedGraph2.edges.foreach(println(_))



  }

}
